﻿// CDialogModify.cpp: 实现文件
//

#include "pch.h"
#include "comAPP2.h"
#include "CDialogModify.h"
#include "afxdialogex.h"

#include "dataStruction.h"

COLORREF tempLineColor;
COLORREF tempFillColor;
COLORREF tempTextColor;
LOGFONT  tempTextFont;

// CDialogModify 对话框

IMPLEMENT_DYNAMIC(CDialogModify, CDialogEx)

CDialogModify::CDialogModify(CWnd* pParent /*=nullptr*/)
	: CDialogEx(IDD_DIALOG1, pParent)
{

}

CDialogModify::~CDialogModify()
{
}

void CDialogModify::DoDataExchange(CDataExchange* pDX)
{
	CDialogEx::DoDataExchange(pDX);
	DDX_Control(pDX, IDC_M_FIGURE_TYPE, EditFigureType);
	DDX_Control(pDX, IDC_M_WIDTH, EditFigureW);
	DDX_Control(pDX, IDC_M_HIGH, EditFigureH);
	DDX_Control(pDX, IDC_M_LINECOLOR, EditLineColor);
	DDX_Control(pDX, IDC_M_FILL_COLOR, EditFillColor);
	DDX_Control(pDX, IDC_EDIT_TEXTSTR, EditTextStr);
	DDX_Control(pDX, IDC_EDIT_TEXT_COLOR, EditTextColor);
}


BEGIN_MESSAGE_MAP(CDialogModify, CDialogEx)
	ON_BN_CLICKED(ID_DELETE, &CDialogModify::OnBnClickedDelete)
	ON_BN_CLICKED(ID_MODIFY, &CDialogModify::OnBnClickedModify)
	ON_BN_CLICKED(IDC_BUTTON_MODIFY_LINECOLOR, &CDialogModify::OnBnClickedButtonModifyLinecolor)
	ON_BN_CLICKED(IDC_BUTTON_MODIFY_FILLCOLOR, &CDialogModify::OnBnClickedButtonModifyFillcolor)
	ON_BN_CLICKED(IDC_BUTTON_MODIFY_FONT, &CDialogModify::OnBnClickedButtonModifyFont)
END_MESSAGE_MAP()


// CDialogModify 消息处理程序


void CDialogModify::OnBnClickedDelete()
{
	// TODO: 在此添加控件通知处理程序代码

	//结束对话框并反会删除
	EndDialog(ID_DELETE);
}



void CDialogModify::OnBnClickedModify()
{
	// TODO: 在此添加控件通知处理程序代码
	//内存回读
	CString tempString;
	int tempWidth = 0;
	int tempHigh = 0;


	if (DialogSelectFigureDecor->SelectFigure->Figure == F_LINE)
	{
		//强转
		CMyLine* Line = dynamic_cast<CMyLine*>(DialogSelectFigureDecor->SelectFigure);
		//回读大小回读的时候用CString ，写的时候用char数组
		EditFigureW.GetWindowTextA(tempString);
		sscanf_s(tempString, "%d", &tempWidth);
		EditFigureH.GetWindowTextA(tempString);
		sscanf_s(tempString, "%d", &tempHigh);
		//位置重设
		POINT now;
		now.x = (Line->ptLS.x + Line->ptLE.x) / 2;
		now.y = (Line->ptLS.y + Line->ptLE.y) / 2;
		//外接矩形重设
		Line->OutRect.left = LimitValue(now.x - tempWidth / 2);
		Line->OutRect.top = LimitValue(now.y - tempHigh / 2);
		Line->OutRect.right = now.x + tempWidth / 2;
		Line->OutRect.bottom = now.y + tempHigh / 2;
		//起末点没设
		if (Line->ptLS.x <= Line->ptLE.x)
		{
			if (Line->ptLS.y <= Line->ptLE.y) //左上右下
			{
				Line->ptLS.x = LimitValue(now.x - tempWidth / 2);
				Line->ptLS.y = LimitValue(now.y - tempHigh / 2);
				Line->ptLE.x = now.x + tempWidth / 2;
				Line->ptLE.y = now.y + tempHigh / 2;
			}
			else  //左下右上
			{
				Line->ptLS.x = LimitValue(now.x - tempWidth / 2);
				Line->ptLS.y = now.y + tempHigh / 2;
				Line->ptLE.x = now.x + tempWidth / 2;
				Line->ptLE.y = LimitValue(now.y - tempHigh / 2);
			}
		}
		else
		{
			if (Line->ptLS.y < Line->ptLE.y)  //右上左下
			{
				Line->ptLS.x = now.x + tempWidth / 2;
				Line->ptLS.y = LimitValue(now.y - tempHigh / 2);
				Line->ptLE.x = LimitValue(now.x - tempWidth / 2);
				Line->ptLE.y = now.y + tempHigh / 2;
			}
			else                //右下左上
			{
				Line->ptLS.x = now.x + tempWidth / 2;
				Line->ptLS.y = now.y + tempHigh / 2;
				Line->ptLE.x = LimitValue(now.x - tempWidth / 2);
				Line->ptLE.y = LimitValue(now.y - tempHigh / 2);
			}
		}
		//回读线的颜色
		Line->lgPen.lopnColor = tempLineColor;
		//无填充色

	}
	else if (DialogSelectFigureDecor->SelectFigure->Figure == F_RECT)
	{
		//强转
		CMyRect* myRECT = dynamic_cast<CMyRect*>(DialogSelectFigureDecor->SelectFigure);
		//回读大小回读的时候用CString ，写的时候用char数组
		EditFigureW.GetWindowTextA(tempString);
		sscanf_s(tempString, "%d", &tempWidth);
		EditFigureH.GetWindowTextA(tempString);
		sscanf_s(tempString, "%d", &tempHigh);
		//位置重设
		POINT now;
		now.x = (myRECT->OutRect.left + myRECT->OutRect.right) / 2;
		now.y = (myRECT->OutRect.top + myRECT->OutRect.bottom) / 2;
		//防止越界
		if (now.x - tempWidth / 2 < 0)
		{
			myRECT->OutRect.left = 0;
		}
		else
		{
			myRECT->OutRect.left = now.x - tempWidth / 2;
		}
		//防止越界
		if (now.y - tempHigh / 2 < 0)
		{
			myRECT->OutRect.top = 0;
		}
		else
		{
			myRECT->OutRect.top = now.y - tempHigh / 2;
		}
		myRECT->OutRect.right = now.x + tempWidth / 2;
		myRECT->OutRect.bottom = now.y + tempHigh / 2;
		//回读线色
		myRECT->lgPen.lopnColor = tempLineColor;
		//回读填充色
		myRECT->brushColor = tempFillColor;

	}
	else if (DialogSelectFigureDecor->SelectFigure->Figure == F_ELLIPSE)
	{
		//强转
		CMyElli* myELLI = dynamic_cast<CMyElli*>(DialogSelectFigureDecor->SelectFigure);
		//回读大小回读的时候用CString ，写的时候用char数组
		EditFigureW.GetWindowTextA(tempString);
		sscanf_s(tempString, "%d", &tempWidth);
		EditFigureH.GetWindowTextA(tempString);
		sscanf_s(tempString, "%d", &tempHigh);
		//位置重设
		POINT now;
		now.x = (myELLI->OutRect.left + myELLI->OutRect.right) / 2;
		now.y = (myELLI->OutRect.top + myELLI->OutRect.bottom) / 2;
		//防止越界
		if (now.x - tempWidth / 2 < 0)
		{
			myELLI->OutRect.left = 0;
		}
		else
		{
			myELLI->OutRect.left = now.x - tempWidth / 2;
		}
		//防止越界
		if (now.y - tempHigh / 2 < 0)
		{
			myELLI->OutRect.top = 0;
		}
		else
		{
			myELLI->OutRect.top = now.y - tempHigh / 2;
		}
		myELLI->OutRect.right = now.x + tempWidth / 2;
		myELLI->OutRect.bottom = now.y + tempHigh / 2;
		//回读线色
		myELLI->lgPen.lopnColor = tempLineColor;
		//回读填充色
		myELLI->brushColor = tempFillColor;
	}
	else if (DialogSelectFigureDecor->SelectFigure->Figure == F_TEXT)
	{
	    CMyText* myText = dynamic_cast<CMyText*>(DialogSelectFigureDecor->SelectFigure);

		//回读大小
		EditFigureW.GetWindowTextA(tempString);
		sscanf_s(tempString, "%d", &tempWidth);
		EditFigureH.GetWindowTextA(tempString);
		sscanf_s(tempString, "%d", &tempHigh);
		//位置重设
		POINT now;
		now.x = (myText->OutRect.left + myText->OutRect.right) / 2;
		now.y = (myText->OutRect.top + myText->OutRect.bottom) / 2;
		//防止越界
		if (now.x - tempWidth / 2 < 0)
		{
			myText->OutRect.left = 0;
		}
		else
		{
			myText->OutRect.left = now.x - tempWidth / 2;
		}
		//防止越界
		if (now.y - tempHigh / 2 < 0)
		{
			myText->OutRect.top = 0;
		}
		else
		{
			myText->OutRect.top = now.y - tempHigh / 2;
		}
		myText->OutRect.right = now.x + tempWidth / 2;
		myText->OutRect.bottom = now.y + tempHigh / 2;

	    //回读字符串
		EditTextStr.GetWindowTextA(myText->MyStr);
		//回读字体颜色
		myText->textColor = tempTextColor;
		//回读字体
		myText->textFont = tempTextFont;

	}

	//结束对话框并反回修改
	EndDialog(ID_MODIFY);
}


BOOL CDialogModify::OnInitDialog()
{
	CDialogEx::OnInitDialog();

	char tempString[50] = { 0 };
	BYTE R, G, B;

	// TODO:  在此添加额外的初始化
	if (DialogSelectFigureDecor->SelectFigure->Figure == F_LINE)
	{
      	//设置图元类型
		EditFigureType.SetWindowTextA("          线");
		//获得大小
		char width[20] = {0};
		char high[20] = {0};
		CRect rect= DialogSelectFigureDecor->SelectFigure->OutRect;
		wsprintf(width, "%d",abs(rect.right - rect.left) );
		wsprintf(high, "%d", abs(rect.bottom - rect.top));
		EditFigureW.SetWindowTextA(width);
		EditFigureH.SetWindowTextA(high);
		//获得线色
		CMyLine* Line = dynamic_cast<CMyLine*>(DialogSelectFigureDecor->SelectFigure);
		R = GetRValue(Line->lgPen.lopnColor);
		G = GetGValue(Line->lgPen.lopnColor);
		B = GetBValue(Line->lgPen.lopnColor);
		wsprintf(tempString, "RGB(%d,%d,%d)", R, G, B);
		EditLineColor.SetWindowTextA(tempString);
		tempLineColor = Line->lgPen.lopnColor;

		//获得填充色
		EditFillColor.SetWindowTextA("  线没有填充色");
	    //线无文本内容
		EditTextStr.SetWindowTextA(" 无");
		//线无文本颜色
		EditTextColor.SetWindowTextA("无");
	}
	else if (DialogSelectFigureDecor->SelectFigure->Figure == F_RECT)
	{
		//强转
		CMyRect* myRECT = dynamic_cast<CMyRect*>(DialogSelectFigureDecor->SelectFigure);

		//设置图元类型
		EditFigureType.SetWindowTextA("          矩形");

		//获得大小
		char width[20] = { 0 };
		char high[20] = { 0 };
		CRect rect = myRECT->OutRect;
		wsprintf(width, "%d", abs(rect.right - rect.left));
		wsprintf(high, "%d", abs(rect.bottom - rect.top));
		EditFigureW.SetWindowTextA(width);
		EditFigureH.SetWindowTextA(high);
		
		//获得线色
		R = GetRValue(myRECT->lgPen.lopnColor);
		G = GetGValue(myRECT->lgPen.lopnColor);
		B = GetBValue(myRECT->lgPen.lopnColor);
		wsprintf(tempString, "RGB(%d,%d,%d)", R, G, B);
		EditLineColor.SetWindowTextA(tempString);
		tempLineColor = myRECT->lgPen.lopnColor;
		
		//获得填充色
		R = GetRValue(myRECT->brushColor);
		G = GetGValue(myRECT->brushColor);
		B = GetBValue(myRECT->brushColor);
		wsprintf(tempString, "RGB(%d,%d,%d)", R, G, B);
		EditFillColor.SetWindowTextA(tempString);
		tempFillColor = myRECT->brushColor;

		//矩形无文本内容
		EditTextStr.SetWindowTextA(" 无");
		//矩形无文本颜色
		EditTextColor.SetWindowTextA("无");

	}
	else if (DialogSelectFigureDecor->SelectFigure->Figure == F_ELLIPSE)
	{
		//强转
		CMyElli * myELLI = dynamic_cast<CMyElli*>(DialogSelectFigureDecor->SelectFigure);

		//设置图元类型
		EditFigureType.SetWindowTextA("          椭圆");

		//获得大小
		char width[20] = { 0 };
		char high[20] = { 0 };
		CRect rect = myELLI->OutRect;
		wsprintf(width, "%d", abs(rect.right - rect.left));
		wsprintf(high, "%d", abs(rect.bottom - rect.top));
		EditFigureW.SetWindowTextA(width);
		EditFigureH.SetWindowTextA(high);
		//获得线色
		R = GetRValue(myELLI->lgPen.lopnColor);
		G = GetGValue(myELLI->lgPen.lopnColor);
		B = GetBValue(myELLI->lgPen.lopnColor);
		wsprintf(tempString, "RGB(%d,%d,%d)", R, G, B);
		EditLineColor.SetWindowTextA(tempString);
		tempLineColor = myELLI->lgPen.lopnColor;

		//获得填充色
		R = GetRValue(myELLI->brushColor);
		G = GetGValue(myELLI->brushColor);
		B = GetBValue(myELLI->brushColor);
		wsprintf(tempString, "RGB(%d,%d,%d)", R, G, B);
		EditFillColor.SetWindowTextA(tempString);
		tempFillColor = myELLI->brushColor;

		//矩形无文本内容
		EditTextStr.SetWindowTextA(" 无");
		//矩形无文本颜色
		EditTextColor.SetWindowTextA("无");

	}
	else if (DialogSelectFigureDecor->SelectFigure->Figure == F_TEXT)
	{
		//强转
		CMyText* myText = dynamic_cast<CMyText*>(DialogSelectFigureDecor->SelectFigure);

		//设置图元类型
		EditFigureType.SetWindowTextA("         文本");

		//获得大小
		char width[20] = { 0 };
		char high[20] = { 0 };
		CRect rect = myText->OutRect;
		wsprintf(width, "%d", abs(rect.right - rect.left));
		wsprintf(high, "%d", abs(rect.bottom - rect.top));
		EditFigureW.SetWindowTextA(width);
		EditFigureH.SetWindowTextA(high);


		//获得当前字体颜色
		EditLineColor.SetWindowTextA("无线色");

		EditFillColor.SetWindowTextA("无填充色");

		//显示文字内容
		EditTextStr.SetWindowTextA(myText->MyStr);
		R = GetRValue(myText->textColor);
		G = GetGValue(myText->textColor);
		B = GetBValue(myText->textColor);
		wsprintf(tempString, "RGB(%d,%d,%d)", R, G, B);
		EditTextColor.SetWindowTextA(tempString);
		//记录当前字体
		tempTextFont = myText->textFont;
		tempTextColor = myText->textColor;
	}


	return TRUE;  // return TRUE unless you set the focus to a control
				  // 异常: OCX 属性页应返回 FALSE
}

//限幅函数，防止数值越界（小于0）
INT LimitValue(int value)
{
	if (value < 0)
		return 0;
	else
		return value;
}

void CDialogModify::OnBnClickedButtonModifyLinecolor()
{
	char tempstring[50] = {0};
	UINT R, G, B;
	// TODO: 在此添加控件通知处理程序代码
	CColorDialog dialog;
	if (dialog.DoModal() == IDOK)
	{
		tempLineColor = dialog.GetColor();
		//更新显示
		R = GetRValue(tempLineColor);
		G = GetGValue(tempLineColor);
		B = GetBValue(tempLineColor);
		wsprintf(tempstring, "RGB(%d,%d,%d)", R, G, B);
		EditLineColor.SetWindowTextA(tempstring);
	}
}


void CDialogModify::OnBnClickedButtonModifyFillcolor()
{
	// TODO: 在此添加控件通知处理程序代码
	char tempstring[50] = { 0 };
	UINT R, G, B;
	CColorDialog dialog;
	if (dialog.DoModal() == IDOK)
	{
		tempFillColor = dialog.GetColor();
		//更新显示
		R = GetRValue(tempFillColor);
		G = GetGValue(tempFillColor);
		B = GetBValue(tempFillColor);
		wsprintf(tempstring, "RGB(%d,%d,%d)", R, G, B);
		EditFillColor.SetWindowTextA(tempstring);
	}
}


void CDialogModify::OnBnClickedButtonModifyFont()
{
	// TODO: 在此添加控件通知处理程序代码
	char tempstring[50] = { 0 };
	UINT R, G, B;
	CFontDialog dialog;
	if (dialog.DoModal() == IDOK)
	{
		//获得对话框选择的字体
		dialog.GetCurrentFont(&tempTextFont);
		//获取字体颜色
		tempTextColor = dialog.GetColor();

		//更新显示
		R = GetRValue(tempTextColor);
		G = GetGValue(tempTextColor);
		B = GetBValue(tempTextColor);
		wsprintf(tempstring, "RGB(%d,%d,%d)", R, G, B);
		EditTextColor.SetWindowTextA(tempstring);

	}
}
